package com.slmn.antlr;

import java.io.IOException;
import java.io.InputStream;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.junit.Test;

import com.slmn.model.SymbolASTFactory;
import com.slmn.model.SymbolTable;
import com.slmn.model.DefPhase;
import com.slmn.model.RefPhase;

public class CheckSymbols {

	@Test
	public void Process() throws IOException {
		InputStream is = CheckSymbols.class.getResource("/Mson.js")
				.openStream();
		ANTLRInputStream input = new ANTLRInputStream(is);
		MsonLexer lexer = new MsonLexer(input);
//		SymbolASTFactory factory = new SymbolASTFactory();
//		lexer.setTokenFactory(factory);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		
		MsonParser parser = new MsonParser(tokens);
//		parser.setTokenFactory(factory);
		parser.addErrorListener(new DiagnosticErrorListener());
		parser.getInterpreter().setPredictionMode(
				PredictionMode.LL_EXACT_AMBIG_DETECTION);
		parser.setBuildParseTree(true);
		ParseTree tree = parser.mson();
		// show tree in text form
		// System.out.println(tree.toStringTree(parser));

		ParseTreeWalker walker = new ParseTreeWalker();
		SymbolTable symtab = new SymbolTable();
		DefPhase def = new DefPhase(symtab);
		walker.walk(def, tree);
		// create next phase and feed symbol table info from def to ref phase
		RefPhase ref = new RefPhase(symtab, def.scopes);
		walker.walk(ref, tree);
	}
}
